﻿using System;
using System.Diagnostics;
using System.Threading;

namespace StopwatchTimer
{
    struct TimingResult
    {
        public TimeSpan Elapsed;

        public int Iterations;

        public string Summary()
        {
            return $"{Elapsed.TotalMilliseconds / Iterations} ms / iteration";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           var timerResult= TestMicroTimer();


            Console.WriteLine($"MicroSecondTimer timer: {timerResult.Summary()}");
         
            Console.ReadKey( );




        }
        private static TimingResult TestMicroTimer()
        {
            TimeSpan total = TimeSpan.Zero;
            int iterations = 0;
            Stopwatch s = new Stopwatch();

            var timer = new MicroSecondTimer(1000);
     
            timer.Enabled = true;

            timer.MicroSecondTimerElapsed += Timer_MicroTimerElapsed;

            s.Start();

            Console.ReadKey(true);


            lock (s)
            {
                return new TimingResult()
                {
                    Elapsed = s.Elapsed,
                    Iterations = iterations
                };
            }
            void Timer_MicroTimerElapsed(object sender, MicroSecondTimerEventArgs timerEventArgs)
            {

                var ts = s.Elapsed;
                lock (s)
                {
                    total += ts;
                    iterations++;
                }
              ///  Thread.Sleep(11);
                 //Console.WriteLine(ts.TotalMilliseconds);
 
            }

        }
    }
}
